package com.woniuxy.business.controller;

import com.woniuxy.business.service.impl.CollateralServiceImpl;
import com.woniuxy.entity.Collateral;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/collateral")
public class CollateralController {
    @Resource
    private CollateralServiceImpl collateralService;

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Collateral collateral){
        collateral.setCollateral_num(UUID.randomUUID().toString().replace("-", ""));
        collateral.setUpdate_time(new Date());
        collateralService.addCollateral(collateral);
        ResponseResult responseResult = new ResponseResult(
                200, ResultStatus.REQUEST_SUCCESS,"添加成功",null);
        return responseResult;
    }
    @GetMapping("/get")
    public ResponseResult<List<Collateral>> get(Collateral collateral, int pageIndex, int pageSize){
        ResponseResult<List<Collateral>> responseResult = new ResponseResult<>(200, ResultStatus.REQUEST_SUCCESS, "查询成功", null);
//        List<Collateral> lists = new ArrayList<>();
//        System.out.println(collateral.getCollateral_num());
//        //按担保物编号查找
//        if(collateral.getCollateral_num()!=null || collateral.getCollateral_num()!=""){
//            Collateral collateralByCollateralNum =
//                    collateralService.getCollateralByCollateralNum(collateral.getCollateral_num());
//            lists.add(collateralByCollateralNum);
//            responseResult.setData(lists);
//        }else{
            List<Collateral> collateralByPage = collateralService.getCollateralByPage(
                    collateral.getCollateral_num(), collateral.getCollateral_type(), pageIndex, pageSize);
            responseResult.setData(collateralByPage);
        return responseResult;
    }
    @DeleteMapping("/delete/{collateral_num}")
    public ResponseResult delete(@PathVariable String collateral_num){
        collateralService.removeCollateral(collateral_num);
        ResponseResult responseResult = new ResponseResult(200, ResultStatus.REQUEST_SUCCESS, "删除成功", null);
        return responseResult;
    }
    @GetMapping("/getTypes")
    public ResponseResult<Set<String>> getTypes(){
        Set<String> types = collateralService.getType();
        ResponseResult<Set<String>> responseResult = new ResponseResult(200, ResultStatus.REQUEST_SUCCESS, "查询成功", types);
        return responseResult;
    }
    @GetMapping("/test")
    public String getTest(){
        return "test";
    }
}
